package org.deri.vor.map;


import org.deri.vor.R;
import org.deri.vor.mobilesensing.globalsensor.ListSensorXMLParser;
import org.deri.vor.mobilesensing.globalsensor.ListSensorXMLParser.Sensor;
import org.deri.vor.mobilesensing.mobilesensor.ListCrowXMLParser;
import org.deri.vor.mobilesensing.mobilesensor.ListPeopleXMLParser;
import org.deri.vor.mobilesensing.mobilesensor.ListCrowXMLParser.Crow;
import org.deri.vor.mobilesensing.mobilesensor.ListPeopleXMLParser.People;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;


public class GalwayMap extends MapView{
	public String Check;
	

	
	String latitudeS,longtitudeS;
	
	SensorOverLay layer;
	SensorOverLay layerCenter;
	Drawable alien, pin, alien1;
	
	GeoPoint Deri,Nuig,Center1, Center2, Metro, Hospital, SouthPark, SalthHill;
	
	
	Sensor	shopStreetCam1,shopStreetCam2, DockCam;
	long start_time;
	
	
	
	public GalwayMap(Context arg0, AttributeSet arg1) {
		super(arg0, arg1);
		
		alien1	 	= getResources().getDrawable(R.drawable.alien);
		alien	 	= getResources().getDrawable(R.drawable.group);
		pin			= getResources().getDrawable(R.drawable.pin);
		
		
		Deri  		 = new GeoPoint(53289551, -9073712);
		Nuig   		 = new GeoPoint(53283548, -9064487);
		Center1		 = new GeoPoint(53271708, -9052710);
		Center2		 = new GeoPoint(53271708, -9046710);
		Metro		 = new GeoPoint(53281488, -9046447);
		Hospital	 = new GeoPoint(53276613, -9067126);
		SouthPark	 = new GeoPoint(53265070, -9053177);
		SalthHill 	 = new GeoPoint(53259060, -9075998);
	}
	
	int lastZoomlvl = 0;
	
	@Override
	public void draw(Canvas canvas){
		
		getOverlays().clear();
		
		if (Check.contains("GPS")){
			if (getZoomLevel()>21){
					MobilePeopleSensorRender();
				
			}else{
					MobileSensorRender();
					
			}
		}
		
		if (Check.contains("GLS")) GlobalSensorRender();
		
		super.draw(canvas);
	}
	

	private void GlobalSensorRender() {
		
		
		
		latitudeS = Double.toString(getMapCenter().getLatitudeE6() / 1E6);
		longtitudeS = Double.toString(getMapCenter().getLongitudeE6() / 1E6);

		String url = "http://lsm.deri.ie/lsmfeed?api=discover&usrId=31865017465746"
						+ "&lat=" + latitudeS + "&long=" + longtitudeS;
		
		System.out.println(url);
		ListSensorXMLParser parser = new ListSensorXMLParser(url);
		
		for (int i = 0; i < parser.sensorList.size(); i++) {
			Sensor sensor = parser.sensorList.get(i);

			if (sensor.getSensorType().equals("bikehire")) {
				
				Drawable drawable = getResources().getDrawable(R.drawable.icon_bikehire);
				
				SensorOverLay sensorOverlay = new SensorOverLay(drawable,sensor.getSensorURL());
				
				GeoPoint point = new GeoPoint(sensor.getLat(), sensor.getLon());
				
				OverlayItem overlayItem = new OverlayItem(point, "Bike Hire", "");
				
				sensorOverlay.addOverlay(overlayItem);

				getOverlays().add(sensorOverlay);
			
			}

			if (sensor.getSensorType().equals("railwaystation")) {
				Drawable drawable = getResources().getDrawable(
						R.drawable.icon_railwaystation);
				SensorOverLay sensorOverlay = new SensorOverLay(drawable,
						sensor.getSensorURL());

				GeoPoint point = new GeoPoint(sensor.getLat(), sensor.getLon());
				OverlayItem overlayItem = new OverlayItem(point, "Railway Station", "");
				sensorOverlay.addOverlay(overlayItem);

				getOverlays().add(sensorOverlay);
			
			}

			if (sensor.getSensorType().equals("traffic")) {
				Drawable drawable = getResources().getDrawable(
						R.drawable.icon_traffic);
				SensorOverLay sensorOverlay = new SensorOverLay(drawable,
						sensor.getSensorURL());

				GeoPoint point = new GeoPoint(sensor.getLat(), sensor.getLon());
				OverlayItem overlayItem = new OverlayItem(point, "Traffic", "");
				sensorOverlay.addOverlay(overlayItem);

				getOverlays().add(sensorOverlay);
			
			}

			if (sensor.getSensorType().equals("weather")) {
				Drawable      drawable 		= getResources().getDrawable(R.drawable.icon_weather);

				SensorOverLay sensorOverlay = new SensorOverLay(drawable, sensor.getSensorURL());
								
				GeoPoint point = new GeoPoint(sensor.getLat(), sensor.getLon());
				
				OverlayItem overlayItem = new OverlayItem(point, "Weather", "");
				
				sensorOverlay.addOverlay(overlayItem);

				getOverlays().add(sensorOverlay);
				
			}

			if (sensor.getSensorType().equals("Webcam")) {
				Drawable drawable = getResources().getDrawable(
						R.drawable.icon_webcam);
				SensorOverLay sensorOverlay = new SensorOverLay(drawable,
						sensor.getSensorURL());

				GeoPoint point = new GeoPoint(sensor.getLat(), sensor.getLon());
				OverlayItem overlayItem = new OverlayItem(point, "Webcam", "");
				sensorOverlay.addOverlay(overlayItem);

				getOverlays().add(sensorOverlay);
			
			}

			if (sensor.getSensorType().equals("airport")) {
				Drawable drawable = getResources().getDrawable(
						R.drawable.icon_airport);
				SensorOverLay sensorOverlay = new SensorOverLay(drawable,
						sensor.getSensorURL());

				GeoPoint point = new GeoPoint(sensor.getLat(), sensor.getLon());
				OverlayItem overlayItem = new OverlayItem(point, "Airport", "");
				sensorOverlay.addOverlay(overlayItem);

				getOverlays().add(sensorOverlay);
			
			}

			if (sensor.getSensorType().equals("sealevel")) {
				Drawable drawable = getResources().getDrawable(
						R.drawable.icon_sealevel);
				SensorOverLay sensorOverlay = new SensorOverLay(drawable,
						sensor.getSensorURL());

				GeoPoint point = new GeoPoint(sensor.getLat(), sensor.getLon());
				OverlayItem overlayItem = new OverlayItem(point, "Sea Level", "");
				sensorOverlay.addOverlay(overlayItem);

				getOverlays().add(sensorOverlay);
		
			}

			if (sensor.getSensorType().equals("Snow Fall")) {
				Drawable drawable = getResources().getDrawable(
						R.drawable.icon_snowfall);
				SensorOverLay sensorOverlay = new SensorOverLay(drawable,
						sensor.getSensorURL());

				GeoPoint point = new GeoPoint(sensor.getLat(), sensor.getLon());
				OverlayItem overlayItem = new OverlayItem(point, "", "");
				sensorOverlay.addOverlay(overlayItem);

				getOverlays().add(sensorOverlay);
	
			}

			if (sensor.getSensorType().equals("snowdepth")) {
				Drawable drawable = getResources().getDrawable(
						R.drawable.icon_snowdepth);
				SensorOverLay sensorOverlay = new SensorOverLay(drawable,
						sensor.getSensorURL());

				GeoPoint point = new GeoPoint(sensor.getLat(), sensor.getLon());
				OverlayItem overlayItem = new OverlayItem(point, "Snow Depth", "");
				sensorOverlay.addOverlay(overlayItem);

				getOverlays().add(sensorOverlay);

			}
		}
	}
	
	private void MobileSensorRender(){
		GeoPoint mapCenter = getMapCenter();
		
		lastZoomlvl = getZoomLevel();
		
		long topLat		 = 	mapCenter.getLatitudeE6() 	+ getLatitudeSpan()/2;
		long topLon  	 =  mapCenter.getLongitudeE6() 	+ getLongitudeSpan()/2;
		long bottomLat	 =  mapCenter.getLatitudeE6() 	- getLatitudeSpan()/2;
		long bottomLon	 =  mapCenter.getLongitudeE6() 	- getLongitudeSpan()/2;
		
		
//		layerCenter = new SensorOverLay(pin);
//		OverlayItem	overlay	=	new OverlayItem(SouthPark,"SouthPark", "");
//		layerCenter.addOverlay(overlay);
//		overlay	=	new OverlayItem(Center2,"Center2", "");
//		layerCenter.addOverlay(overlay);
//		overlay	=	new OverlayItem(Center1,"Center1", "");
//		layerCenter.addOverlay(overlay);
//		overlay	=	new OverlayItem(Deri,"Deri", "");
//		layerCenter.addOverlay(overlay);
//		overlay	=	new OverlayItem(Nuig,"Nuig", "");
//		layerCenter.addOverlay(overlay);
	
//		http://lsm.deri.ie/volvorace/oceanrace?api=discover&userId=ff808181365715d701365718c1a30001&lat=53.273671&long=-9.049615&neLat=53.278316&neLong=-9.044466&swLat=53.269026&swLong=-9.054764&zoomLevel=19
	    
		SensorOverLay layer    = new SensorOverLay(alien1);
		SensorOverLay layer1		= new SensorOverLay(alien);
	    String url = "http://lsm.deri.ie/volvorace/oceanrace?api=discover&userId=ff808181365715d701365718c1a30001" +
	    			 "&lat="    + mapCenter.getLatitudeE6()/1E6 	+ 
	    			 "&long="   + mapCenter.getLongitudeE6()/1E6 	+
	    			 "&neLat="  + topLat/1E6 						+
	    			 "&neLong=" + topLon/1E6 						+
	    			 "&swLat="  + bottomLat/1E6 					+
	    			 "&swLong=" + bottomLon/1E6 					+
	    			 "&zoomLevel=" + getZoomLevel() ;
	    
	    ListCrowXMLParser parser	=	new ListCrowXMLParser(url);
		
	    getOverlays().clear();
		for (int i=0; i<parser.crowList.size(); i++){
	    	Crow crow	    	=	parser.crowList.get(i);
	    	GeoPoint point		=   new GeoPoint(crow.getLat(),crow.getLon());
	    	if (crow.getNumber().equals("01"))
	    	{
	    		OverlayItem overlay	=	new OverlayItem(point,crow.getNumber(), "");
	    		layer.addOverlay(overlay);
	    	}
	    	else
	    	{
	    		OverlayItem overlay	=	new OverlayItem(point,crow.getNumber(), "");
	    		layer1.addOverlay(overlay);
	    	}
		}	
	    
		if (layer.size()>0) {  getOverlays().add(layer);}
		if (layer1.size()>0){	getOverlays().add(layer1);
			}
		
//		getOverlays().add(layerCenter);
	}	

		
	private void MobilePeopleSensorRender(){
		GeoPoint mapCenter = getMapCenter();
		
		OverlayItem overlay;
		
		layer	=	new SensorOverLay(alien);
		
	    String    url	=	"http://lsm.deri.ie/volvorace/oceanrace?api=people" +
	    					"&lat="  +	mapCenter.getLatitudeE6()/1E6 +
	    					"&long=" +  mapCenter.getLongitudeE6()/1E6;
	    
	    System.out.println(url);
	
	    ListPeopleXMLParser parser	=	new ListPeopleXMLParser(url);
	    	
	    getOverlays().clear();
		
		for (int i=0; i<parser.peopleList.size(); i++){
	    	People people	    =	parser.peopleList.get(i);
	    	GeoPoint point		=	new GeoPoint(people.getLat(),people.getLon());
	    	overlay				=	new OverlayItem(point,parser.peopleList.get(i).getNumber(), "");
	    	layer.addOverlay(overlay);
	    }	
	    
		
		if (parser.peopleList.size() != 0)
			getOverlays().add(layer);
	    
//		getOverlays().add(layerCenter);
	}	
	
	

//	public void level18_19(){
//		if ((System.currentTimeMillis()-start_time)<400) return;
//		start_time	=	System.currentTimeMillis();
//		
//		layer		=	new SensorOverLay(alien12);
//		getOverlays().clear();
//		//Around DERI
//		int topLat	=	Deri.getLatitudeE6() + getLatitudeSpan()/4;
//	    int topLon  =   Deri.getLongitudeE6() + getLongitudeSpan()/4;
//	    int bottomLat = Deri.getLatitudeE6() - getLatitudeSpan()/4;
//	    int bottomLon = Deri.getLongitudeE6() - getLongitudeSpan()/4;
//		
//		for (int i=0;i<13;i++){
//			int x = bottomLat + (int)(Math.random()*(topLat - bottomLat) + 1);
//			int y = bottomLon + (int)(Math.random()*(topLon - bottomLon) + 1);
//			GeoPoint p = new GeoPoint(x,y);
//			OverlayItem overlay = new OverlayItem(p	,"*","");
//			layer.addOverlay(overlay);
//		}
//		
//		//Around Nuig
//		topLat	=	Nuig.getLatitudeE6() + getLatitudeSpan()/4;
//	    topLon  =   Nuig.getLongitudeE6() + getLongitudeSpan()/4;
//	    bottomLat = Nuig.getLatitudeE6() - getLatitudeSpan()/4;
//	    bottomLon = Nuig.getLongitudeE6() - getLongitudeSpan()/4;
//		
//		for (int i=0;i<13;i++){
//			int x = bottomLat + (int)(Math.random()*(topLat - bottomLat) + 1);
//			int y = bottomLon + (int)(Math.random()*(topLon - bottomLon) + 1);
//			GeoPoint p = new GeoPoint(x,y);
//			OverlayItem overlay = new OverlayItem(p	,"*","");
//			layer.addOverlay(overlay);
//		}
//		
//		//Around Center
//		topLat	=	Center1.getLatitudeE6() + getLatitudeSpan()/4;
//	    topLon  =   Center1.getLongitudeE6() + getLongitudeSpan()/4;
//	    bottomLat = Center1.getLatitudeE6() - getLatitudeSpan()/4;
//	    bottomLon = Center1.getLongitudeE6() - getLongitudeSpan()/4;
//		
//		for (int i=0;i<13;i++){
//			int x = bottomLat + (int)(Math.random()*(topLat - bottomLat) + 1);
//			int y = bottomLon + (int)(Math.random()*(topLon - bottomLon) + 1);
//			GeoPoint p = new GeoPoint(x,y);
//			OverlayItem overlay = new OverlayItem(p	,"*","");
//			layer.addOverlay(overlay);
//		}
//
//		//Around Metro
//		topLat	=	Metro.getLatitudeE6() + getLatitudeSpan()/4;
//	    topLon  =   Metro.getLongitudeE6() + getLongitudeSpan()/4;
//	    bottomLat = Metro.getLatitudeE6() - getLatitudeSpan()/4;
//	    bottomLon = Metro.getLongitudeE6() - getLongitudeSpan()/4;
//		
//		for (int i=0;i<13;i++){
//			int x = bottomLat + (int)(Math.random()*(topLat - bottomLat) + 1);
//			int y = bottomLon + (int)(Math.random()*(topLon - bottomLon) + 1);
//			GeoPoint p = new GeoPoint(x,y);
//			OverlayItem overlay = new OverlayItem(p	,"*","");
//			layer.addOverlay(overlay);
//		}
//		
//		//Around Hospital
//		topLat	=	Hospital.getLatitudeE6() + getLatitudeSpan()/4;
//	    topLon  =   Hospital.getLongitudeE6() + getLongitudeSpan()/4;
//	    bottomLat = Hospital.getLatitudeE6() - getLatitudeSpan()/4;
//	    bottomLon = Hospital.getLongitudeE6() - getLongitudeSpan()/4;
//		
//		for (int i=0;i<13;i++){
//			int x = bottomLat + (int)(Math.random()*(topLat - bottomLat) + 1);
//			int y = bottomLon + (int)(Math.random()*(topLon - bottomLon) + 1);
//			GeoPoint p = new GeoPoint(x,y);
//			OverlayItem overlay = new OverlayItem(p	,"*","");
//			layer.addOverlay(overlay);
//		}
//		
//		//Around SouthPark
//		topLat	=	SouthPark.getLatitudeE6() + getLatitudeSpan()/4;
//	    topLon  =   SouthPark.getLongitudeE6() + getLongitudeSpan()/4;
//	    bottomLat = SouthPark.getLatitudeE6() - getLatitudeSpan()/4;
//	    bottomLon = SouthPark.getLongitudeE6() - getLongitudeSpan()/4;
//		
//		for (int i=0;i<13;i++){
//			int x = bottomLat + (int)(Math.random()*(topLat - bottomLat) + 1);
//			int y = bottomLon + (int)(Math.random()*(topLon - bottomLon) + 1);
//			GeoPoint p = new GeoPoint(x,y);
//			OverlayItem overlay = new OverlayItem(p	,"*","");
//			layer.addOverlay(overlay);
//		}
//		
//		//Around SalthHill
//		topLat	=	SalthHill.getLatitudeE6() + getLatitudeSpan()/4;
//	    topLon  =   SalthHill.getLongitudeE6() + getLongitudeSpan()/4;
//	    bottomLat = SalthHill.getLatitudeE6() - getLatitudeSpan()/4;
//	    bottomLon = SalthHill.getLongitudeE6() - getLongitudeSpan()/4;
//		
//		for (int i=0;i<13;i++){
//			int x = bottomLat + (int)(Math.random()*(topLat - bottomLat) + 1);
//			int y = bottomLon + (int)(Math.random()*(topLon - bottomLon) + 1);
//			GeoPoint p = new GeoPoint(x,y);
//			OverlayItem overlay = new OverlayItem(p	,"*","");
//			layer.addOverlay(overlay);
//		}
//		
//		getOverlays().add(layer);
//	}
	
//	public void level17(){
//		if ((System.currentTimeMillis()-start_time)<2*1000) return;
//		start_time	=	System.currentTimeMillis();
//		layer		=	new SensorOverLay(alien);
//		getOverlays().clear();
//		//Around DERI
//		int topLat	=	Deri.getLatitudeE6() + getLatitudeSpan()/10;
//	    int topLon  =   Deri.getLongitudeE6() + getLongitudeSpan()/10;
//	    int bottomLat = Deri.getLatitudeE6() - getLatitudeSpan()/10;
//	    int bottomLon = Deri.getLongitudeE6() - getLongitudeSpan()/10;
//		
//		for (int i=0;i<5;i++){
//			int x = bottomLat + (int)(Math.random()*(topLat - bottomLat) + 1);
//			int y = bottomLon + (int)(Math.random()*(topLon - bottomLon) + 1);
//			GeoPoint p = new GeoPoint(x,y);
//			OverlayItem overlay = new OverlayItem(p	,"","");
//			layer.addOverlay(overlay);
//		}
//		
//		//Around Nuig
//		topLat	=	Nuig.getLatitudeE6() + getLatitudeSpan()/10;
//	    topLon  =   Nuig.getLongitudeE6() + getLongitudeSpan()/10;
//	    bottomLat = Nuig.getLatitudeE6() - getLatitudeSpan()/10;
//	    bottomLon = Nuig.getLongitudeE6() - getLongitudeSpan()/10;
//		
//		for (int i=0;i<5;i++){
//			int x = bottomLat + (int)(Math.random()*(topLat - bottomLat) + 1);
//			int y = bottomLon + (int)(Math.random()*(topLon - bottomLon) + 1);
//			GeoPoint p = new GeoPoint(x,y);
//			OverlayItem overlay = new OverlayItem(p	,"","");
//			layer.addOverlay(overlay);
//		}
//		//Around Center
//		topLat	=	Center1.getLatitudeE6() + getLatitudeSpan()/10;
//	    topLon  =   Center1.getLongitudeE6() + getLongitudeSpan()/10;
//	    bottomLat = Center1.getLatitudeE6() - getLatitudeSpan()/10;
//	    bottomLon = Center1.getLongitudeE6() - getLongitudeSpan()/10;
//		
//		for (int i=0;i<5;i++){
//			int x = bottomLat + (int)(Math.random()*(topLat - bottomLat) + 1);
//			int y = bottomLon + (int)(Math.random()*(topLon - bottomLon) + 1);
//			GeoPoint p = new GeoPoint(x,y);
//			OverlayItem overlay = new OverlayItem(p	,"","");
//			layer.addOverlay(overlay);
//		}
//
//		//Around Metro
//		topLat	=	Metro.getLatitudeE6() + getLatitudeSpan()/10;
//	    topLon  =   Metro.getLongitudeE6() + getLongitudeSpan()/10;
//	    bottomLat = Metro.getLatitudeE6() - getLatitudeSpan()/10;
//	    bottomLon = Metro.getLongitudeE6() - getLongitudeSpan()/10;
//		
//		for (int i=0;i<5;i++){
//			int x = bottomLat + (int)(Math.random()*(topLat - bottomLat) + 1);
//			int y = bottomLon + (int)(Math.random()*(topLon - bottomLon) + 1);
//			GeoPoint p = new GeoPoint(x,y);
//			OverlayItem overlay = new OverlayItem(p	,"","");
//			layer.addOverlay(overlay);
//		}
//		
//		//Around Hospital
//		topLat	=	Hospital.getLatitudeE6() + getLatitudeSpan()/12;
//	    topLon  =   Hospital.getLongitudeE6() + getLongitudeSpan()/12;
//	    bottomLat = Hospital.getLatitudeE6() - getLatitudeSpan()/12;
//	    bottomLon = Hospital.getLongitudeE6() - getLongitudeSpan()/12;
//		
//		for (int i=0;i<5;i++){
//			int x = bottomLat + (int)(Math.random()*(topLat - bottomLat) + 1);
//			int y = bottomLon + (int)(Math.random()*(topLon - bottomLon) + 1);
//			GeoPoint p = new GeoPoint(x,y);
//			OverlayItem overlay = new OverlayItem(p	,"","");
//			layer.addOverlay(overlay);
//		}
//		
//		//Around SouthPark
//		topLat	=	SouthPark.getLatitudeE6() + getLatitudeSpan()/10;
//	    topLon  =   SouthPark.getLongitudeE6() + getLongitudeSpan()/10;
//	    bottomLat = SouthPark.getLatitudeE6() - getLatitudeSpan()/10;
//	    bottomLon = SouthPark.getLongitudeE6() - getLongitudeSpan()/10;
//		
//		for (int i=0;i<5;i++){
//			int x = bottomLat + (int)(Math.random()*(topLat - bottomLat) + 1);
//			int y = bottomLon + (int)(Math.random()*(topLon - bottomLon) + 1);
//			GeoPoint p = new GeoPoint(x,y);
//			OverlayItem overlay = new OverlayItem(p	,"","");
//			layer.addOverlay(overlay);
//		}
//		
//		//Around SalthHill
//		topLat	=	SalthHill.getLatitudeE6() + getLatitudeSpan()/10;
//	    topLon  =   SalthHill.getLongitudeE6() + getLongitudeSpan()/10;
//	    bottomLat = SalthHill.getLatitudeE6() - getLatitudeSpan()/10;
//	    bottomLon = SalthHill.getLongitudeE6() - getLongitudeSpan()/10;
//		
//		for (int i=0;i<5;i++){
//			int x = bottomLat + (int)(Math.random()*(topLat - bottomLat) + 1);
//			int y = bottomLon + (int)(Math.random()*(topLon - bottomLon) + 1);
//			GeoPoint p = new GeoPoint(x,y);
//			OverlayItem overlay = new OverlayItem(p	,"","");
//			layer.addOverlay(overlay);
//		}
//		
//		
//		getOverlays().add(layer);
//	}
//	
}
